package com.hina.rec.application.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.idev.excel.FastExcel;
import cn.idev.excel.event.SyncReadListener;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hina.rec.application.convert.SystemProductConvert;
import com.hina.rec.application.manager.SettleSysProductManager;
import com.hina.rec.application.service.SystemProductService;
import com.hina.rec.common.utils.AssertUtil;
import com.hina.rec.domain.model.SettleSysProductDO;
import com.hina.rec.endpoint.req.FileUploadReq;
import com.hina.rec.endpoint.req.SysProductCompareReq;
import com.hina.rec.endpoint.req.SysProductSearchReq;
import com.hina.rec.endpoint.res.SysProductCompareRes;
import com.hina.rec.endpoint.res.SysProductRes;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 系统产品服务实现类
 *
 * @author liaosp
 * @since 2025-05-23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SystemProductServiceImpl implements SystemProductService {

    private final SettleSysProductManager settleSysProductManager;

    @Override
    public String importExcel(FileUploadReq fileUploadReq) {
        AssertUtil.notNull(fileUploadReq, "文件上传请求不能为空");
        MultipartFile file = fileUploadReq.getFile();
        AssertUtil.notNull(file, "上传文件不能为空");

        try {
            List<SettleSysProductDO> dataList = FastExcel.read(file.getInputStream(), SettleSysProductDO.class,
                    new SyncReadListener()).doReadAllSync();
            AssertUtil.notEmpty(dataList, "导入数据不能为空");
            // 批量保存数据
            boolean success = settleSysProductManager.saveBatch(dataList);
            return success ? "导入成功，共导入" + dataList.size() + "条数据" : "导入失败";
        } catch (IOException e) {
            log.error("导入系统产品数据失败", e);
            throw new RuntimeException("导入系统产品数据失败: " + e.getMessage());
        }
    }

    @Override
    public List<SysProductRes> search(SysProductSearchReq req) {
        AssertUtil.notNull(req, "查询参数不能为空");
        AssertUtil.notNull(req.getMonth(), "月份不能为空");
        // 设置查询条件
        LambdaQueryChainWrapper<SettleSysProductDO> queryWrapper = settleSysProductManager.lambdaQuery()
                .eq(Objects.nonNull(req.getYear()), SettleSysProductDO::getYear, req.getYear())
                .eq(Objects.nonNull(req.getMonth()), SettleSysProductDO::getMonth, req.getMonth())
                .eq(StrUtil.isNotBlank(req.getSettleName()), SettleSysProductDO::getSettleName, req.getSettleName())
                .like(StrUtil.isNotBlank(req.getBatchName()), SettleSysProductDO::getBatchName, req.getBatchName())
                .eq(StrUtil.isNotBlank(req.getSencMemberId()), SettleSysProductDO::getSencMemberId, req.getSencMemberId())
                .eq(StrUtil.isNotBlank(req.getSubProductType()), SettleSysProductDO::getSubProductType, req.getSubProductType())
                .eq(StrUtil.isNotBlank(req.getSource()), SettleSysProductDO::getSource, req.getSource());
        // 查询数据
        List<SettleSysProductDO> list = settleSysProductManager.list(queryWrapper);
        // 转换为响应对象
        return list.stream().map(SystemProductConvert.INSTANCE::toRes).collect(Collectors.toList());
    }

    @Override
    public List<SysProductCompareRes> compare(SysProductCompareReq req) {
        AssertUtil.notNull(req, "对比参数不能为空");
        AssertUtil.notNull(req.getSourceMonth(), "源月份不能为空");
        AssertUtil.notNull(req.getTargetMonth(), "目标月份不能为空");
        return null;
    }

    /**
     * 获取产品唯一标识键
     *
     * @param product 产品数据
     * @return 唯一标识键
     */
    private String getProductKey(SettleSysProductDO product) {
        return product.getSencMemberId() + "_" + product.getSubProductType() + "_" + product.getSettleName();
    }
}